﻿@inject IStringLocalizer<DropdownCascade> Localizer

<div class="row g-3">
    <div class="col-12 col-sm-6">
        <Dropdown TValue="string" Items="Items3" OnSelectedItemChanged="OnCascadeBindSelectClick" />
    </div>
    <div class="col-12 col-sm-6">
        <Dropdown TValue="string" Items="Items2" />
    </div>
</div>

@code {
    private IEnumerable<SelectedItem>? Items2 { get; set; }

    private IEnumerable<SelectedItem>? Items3 = new SelectedItem[] { };

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Items3 = new SelectedItem[]
        {
            new SelectedItem ("", Localizer["Item1"]),
            new SelectedItem ("Beijing", Localizer["Item2"]) { Active = true },
            new SelectedItem ("Shanghai", Localizer["Item3"]),
            new SelectedItem ("Hangzhou", Localizer["Item4"])
        };
    }

    /// <summary>
    /// 级联绑定菜单
    /// </summary>
    /// <param name="item"></param>
    private async Task OnCascadeBindSelectClick(SelectedItem item)
    {
        // 模拟异步通讯切换线程
        await Task.Delay(10);
        if (item.Value == "Beijing")
        {
            Items2 = new SelectedItem[]
            {
                new SelectedItem("1",Localizer["Item5"]) { Active = true },
                new SelectedItem("2",Localizer["Item6"]),
            };
        }
        else if (item.Value == "Shanghai")
        {
            Items2 = new SelectedItem[]
            {
                new SelectedItem("1",Localizer["Item7"]),
                new SelectedItem("2",Localizer["Item8"]) { Active = true } ,
            };
        }
        else
        {
            Items2 = Enumerable.Empty<SelectedItem>();
        }
        StateHasChanged();
    }
}
